package com.hrbxlh.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hrbxlh.actionImpl.CourseActionImpl;
import com.hrbxlh.actionImpl.MyCourseActionImpl;
import com.hrbxlh.actionImpl.QuestionActionImpl;


/**
* -------------------------------------------------------
* Copyright (c) 2017, 鑫联华信息科技股份有限公司
* All rights reserved.
* FileName：SourceAction.java
* Description：题库action类
* History：
* Date           Author               Desc
* 2017.4.20		  sy                   建立
* -------------------------------------------------------
*/

@Controller
public class QuestionAction {

	@Autowired
	@Qualifier("questionActionImpl")
	private QuestionActionImpl questionActionImpl;
	
	@Autowired
	@Qualifier("courseActionImpl")
	private CourseActionImpl courseActionImpl;
	
	@Autowired
	@Qualifier("myCourseActionImpl")
	private MyCourseActionImpl myCourseActionImpl;
	
	/**
	 * @param pageNow,pageSize(一般是10)，页数从1开始，第一页pageNow传1
	 * @return  题库列表页面。进页面，翻页，条件查询都是这个。
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getAllQuestionList", method = RequestMethod.GET)
	public ModelAndView getAllQuestionList(String jsonStr, HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(MemberAction.class.getName());
		try {
			//模拟参数开始
//			Map<String, Object> mapUse = new HashMap<String, Object>();
//			mapUse.put("pageNow", 1);
//			mapUse.put("pageSize", 10);
//			jsonStr = mapper.writeValueAsString(mapUse);
			//模拟参数结束
			
			//获取页面参数值
			Map<String, Object> preParamMap = mapper.readValue(jsonStr, returnMap.getClass());
			//获取题库列表信息
			Map<String, Object> resultAllMap = questionActionImpl.getAllQuestionList(preParamMap,session);
			if(String.valueOf(resultAllMap.get("flag")).equals("success")){
				if(preParamMap.get("courseId")!=null||preParamMap.get("courseId")!=null){
					//搜素框赋值对象
					Map<String,Object> searchObejct = new HashMap<String,Object>();
					searchObejct.put("courseId", preParamMap.get("courseId"));
					resultMap.put("searchObejct", searchObejct);
				}
				
				//查询当前人的消息
				List<Map<String, Object>> noticeList = courseActionImpl.getMyNotice(request);
				resultMap.put("noticeList", noticeList);
//				String a = "%3Cdiv%20style%3D%22font-size%3A30px%3B%22%3E%u8428%u62C9%u4E01%3C/div%3E";
				//返回值
				resultMap.put("questionList", resultAllMap.get("questionList"));
				resultMap.put("courseListAll", resultAllMap.get("courseListAll"));
				resultMap.put("courseList", resultAllMap.get("courseList"));
				resultMap.put("stuorteaId", resultAllMap.get("stuorteaId"));
				resultMap.put("totalPage", resultAllMap.get("totalPage"));
				resultMap.put("pageNow", resultAllMap.get("pageNow"));
				resultMap.put("pageSize", preParamMap.get("pageSize"));
//				resultMap.put("a", a);
			    //人员列表页面名称
				pageName = "pc/questionList";
			}else{
				resultMap.put("pageNow", 1);
				resultMap.put("pageSize", 10);
				resultMap.put("totalPage", 0);
				resultMap.put("errMsg", resultAllMap.get("errMsg"));
				pageName = "pc/questionList";
			}
			
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("跳转题库列表页面出现异常", e);
			pageName = "public/pageerror";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
			System.out.println("返回的  :" + reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}
	
	/**
	 * @param pageNow,pageSize(一般是10)，页数从1开始，第一页pageNow传1
	 * @return  题库列表页面。进页面，翻页，条件查询都是这个。
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getAllQuestionListByPost", method = RequestMethod.POST)
	public void getAllQuestionListByPost(String jsonStr, HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(MemberAction.class.getName());
		try {
			//模拟参数开始
//			Map<String, Object> mapUse = new HashMap<String, Object>();
//			mapUse.put("pageNow", 1);
//			mapUse.put("pageSize", 10);
//			jsonStr = mapper.writeValueAsString(mapUse);
			//模拟参数结束
			
			//获取页面参数值
			Map<String, Object> preParamMap = mapper.readValue(jsonStr, returnMap.getClass());
			//获取题库列表信息
			Map<String, Object> resultAllMap = questionActionImpl.getAllQuestionList(preParamMap,session);
			if(preParamMap.get("courseId")!=null||preParamMap.get("courseId")!=null){
				//搜素框赋值对象
				Map<String,Object> searchObejct = new HashMap<String,Object>();
				searchObejct.put("courseId", preParamMap.get("courseId"));
				resultMap.put("searchObejct", searchObejct);
			}
			
			//返回值
			resultMap.put("questionList", resultAllMap.get("questionList"));
			resultMap.put("courseList", resultAllMap.get("courseList"));
			resultMap.put("stuorteaId", resultAllMap.get("stuorteaId"));
			resultMap.put("totalPage", resultAllMap.get("totalPage"));
			resultMap.put("pageNow", resultAllMap.get("pageNow"));
			resultMap.put("pageSize", preParamMap.get("pageSize"));
		} catch (Exception e) {
			resultMap.put("errMsg", "跳转页面失败");
			e.printStackTrace();
			logger.error("跳转题库列表页面出现异常", e);
			pageName = "public/pageerror";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
		
	}
	
	/**
	 * @param request
	 * @param response
	 * @return  添加问题表单提交。
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/addQuestion", method = RequestMethod.POST)
	public void addQuestion(String jsonStr, HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(ClassAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try {
			
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//成绩设置。
			boolean resultFlag = questionActionImpl.addQuestion(paramMap);
			resultMark = (resultFlag) ? "success" : "fail";
			resultMap.put("result", resultMark);
			if(resultMark.equals("fail")){
				resultMap.put("errorMsg", "回答问题失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("添加问题出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	/**
	 * @param request
	 * @param response
	 * @return  删除题库,传回题库id集合。List<String>形式   jsonStr = ["1","2","3"...]
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/delQuestions", method = RequestMethod.POST)
	public void delQuestions(String jsonStr, HttpServletRequest request, HttpServletResponse response) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(MemberAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		//用于转换参数
		List<String> list = new ArrayList<String>();
		try {
			/*模拟参数开始*/
			//List<Integer> listUse = new ArrayList<Integer>();
			//listUse.add(2);
			//listUse.add(3);
			//jsonStr = mapper.writeValueAsString(listUse);
			/*模拟参数结束*/
			
			//获取参数值
			List<String> paramList = mapper.readValue(jsonStr, list.getClass());
            //删除人员
			boolean flag = questionActionImpl.delQuestions(paramList);
			resultMark = (flag == true) ? "success" : "fail";
			resultMap.put("result", resultMark);
			
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("删除人员出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	/**
	 * 导入题
	 * @param file
	 * @param response
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/imQuestion", method=RequestMethod.POST)
	public ModelAndView imQuestion(@RequestParam("file") CommonsMultipartFile file, HttpServletResponse response, HttpServletRequest request) throws Exception {
		 System.out.println("导入题");
		 String fileName =  file.getOriginalFilename();
		 String view ="";
		 String reJsonStr = "";
		 ObjectMapper mapper = new ObjectMapper();
		 Map<String,Object> map = new HashMap<String,Object>();
		String jstr="%7B%22pageNow%22:1,%22pageSize%22:10%7D";
		//日志信息
		Logger logger = Logger.getLogger(ClassAction.class.getName());
		 try{
			 
			  String path = request.getSession().getServletContext().getRealPath("/upload/" + fileName);// 存放位置
			  File destFile = new File(path);
			  System.out.println(destFile);
			  String file1 = String.valueOf(destFile);
			  FileUtils.copyInputStreamToFile(file.getInputStream(), destFile);// 复制临时文件到指定目录下
			  Map<String,Object> returnMap = questionActionImpl.imQuestion(file1);
			  if(((String)returnMap.get("flag")).equals("success")){
				  view = "redirect:/getAllQuestionList?jsonStr="+jstr;
			  }else{
				  map.put("errMsg", returnMap.get("errMsg"));
				  view = "public/pageerror";
			  }
			  
		 }catch(Exception e){
			e.printStackTrace();
			map.put("errMsg", "导入题失败");
			e.printStackTrace();
			logger.error("导入题失败", e);
			view = "public/pageerror";
			
		 }finally{
			reJsonStr = mapper.writeValueAsString(map);
			map.put("result", reJsonStr);
		 }

		 return new ModelAndView(view,map);
	}
	
	/**
	 * @param request
	 * @param response
	 * @return  查询所需要生成的试题题库中是否存在
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getQuestionsByExcel", method = RequestMethod.POST)
	public void getQuestionsByExcel(String jsonStr, HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//日志信息
		Logger logger = Logger.getLogger(MemberAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		//用于转换参数
		List<String> list = new ArrayList<String>();
		try {
			/*模拟参数开始*/
			//List<Integer> listUse = new ArrayList<Integer>();
			//listUse.add(2);
			//listUse.add(3);
			//jsonStr = mapper.writeValueAsString(listUse);
			/*模拟参数结束*/
			
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, returnMap.getClass());
            //查询所选试题类型题库中是否存在
			 resultMap = questionActionImpl.getQuestionsByExcel(paramMap,session);
			
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("查询所需要生成的试题题库中是否存在出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	//#########################################2.0####################################################//
	
	/**
	 * @param request
	 * @param response
	 * @return  在线预览
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/onlineGetQuestions", method = RequestMethod.POST)
	public void onlineGetQuestions(String jsonStr, HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//日志信息
		Logger logger = Logger.getLogger(MemberAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		//用于转换参数
		//存放结果值
		Map<String,Object> paraMap = new HashMap<String,Object>();
		try {
			/*模拟参数开始*/
			//List<Integer> listUse = new ArrayList<Integer>();
			//listUse.add(2);
			//listUse.add(3);
			//jsonStr = mapper.writeValueAsString(listUse);
			/*模拟参数结束*/
			
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, returnMap.getClass());
            //查询所选试题
			System.out.println("前台传的参数"+paramMap);
		    resultMap = questionActionImpl.getAllQuestionListExcel(paramMap,session);  
		    resultMap.put("paramMap", paramMap);
		    resultMap.put("flag", true);
			
		} catch (Exception e) {
			resultMap.put("flag", false);
			e.printStackTrace();
			logger.error("在线预览存在出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	/**
	 * @param request
	 * @param response
	 * @return  在线预览试题表单提交。
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/addOnlineQuestion", method = RequestMethod.POST)
	public void addOnlineQuestion(String jsonStr, HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(ClassAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try {
			
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//在线预览表单提交。
			boolean resultFlag = questionActionImpl.addOnlineQuestion(paramMap,session);
			resultMark = (resultFlag) ? "success" : "fail";
			resultMap.put("result", resultMark);
			if(resultMark.equals("fail")){
				resultMap.put("errorMsg", "回答问题失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("添加问题出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	/**
	 * @param 学生在线考试（学号）
	 * @return  该学生的当前所属的所有课程
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getAllStuCourse", method = RequestMethod.GET)
	public ModelAndView getAllStuCourse(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(MemberAction.class.getName());
		try {
			//模拟参数开始
//			Map<String, Object> mapUse = new HashMap<String, Object>();
//			mapUse.put("pageNow", 1);
//			mapUse.put("pageSize", 10);
//			jsonStr = mapper.writeValueAsString(mapUse);
			//模拟参数结束
			
			//获取页面参数值
			Map<String, Object> preParamMap = new HashMap<String,Object>();
			//获取当前登陆人的学号
			String stuorteaId = (String) session.getAttribute("stuorteaId");
			preParamMap.put("stuorteaId", stuorteaId);
			//通过学生获取此学生所属课程
			List<Map<String,Object>> courseList = myCourseActionImpl.getStuCourse(preParamMap);
			resultMap.put("courseList", courseList);
		    //人员列表页面名称
			pageName = "pc/stuonlinequestion";
			
		} catch (Exception e) {
			resultMap.put("errMsg", "学生在线考试页面出现异常");
			e.printStackTrace();
			logger.error("学生在线考试页面出现异常", e);
			pageName = "public/pageerror";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
			System.out.println("返回的  :" + reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}
	
	/**
	 * @param request
	 * @param response
	 * @return  学生在线考试保存提交
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/stuOnlineSubmitExam", method = RequestMethod.POST)
	public void stuOnlineSubmitExam(String jsonStr, HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(ClassAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		
//		List<Map<String,Object>> paramList = new ArrayList<Map<String,Object>>();
		try {
			
			//模拟参数开始
//			List<Map<String,Object>> mapList = new ArrayList<Map<String,Object>>();
//			Map<String, Object> mapUse = new HashMap<String, Object>();
//			Map<String, Object> finalmapUse = new HashMap<String, Object>();
//			mapUse.put("questionId", 40);
//			mapUse.put("ansquesContent", "a");
//			mapList.add(mapUse);
//			mapUse.put("questionId", 41);
//			mapUse.put("ansquesContent", "b");
//			mapList.add(mapUse);
//			finalmapUse.put("mapList", mapList);
//			finalmapUse.put("onlinequestionId", 1);
//			finalmapUse.put("optionSource", 1);
//			finalmapUse.put("judgeSource", 2);
//			finalmapUse.put("courseId", 7);
//			jsonStr = mapper.writeValueAsString(finalmapUse);
			//模拟参数结束
			jsonStr = URLDecoder.decode(jsonStr, "utf-8");
			System.out.println("转码后的json"+jsonStr);
			//获取参数值
			Map<String,Object> param = mapper.readValue(jsonStr, resultMap.getClass());
			System.out.println("前台传的参数"+param);
			//将学生答案插入到数据库
			Map<String,Object> returnMap = questionActionImpl.addStuAnswerQuestion(param,session);
			resultMap.put("flag", returnMap.get("flag"));
			
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(" 学生在线考试ajax请求出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	/**
	 * @param request
	 * @param response
	 * @return  学生在线考试ajax请求试题(前台传课程号)
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/stuOnlineExam", method = RequestMethod.POST)
	public void stuOnlineExam(String jsonStr, HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(ClassAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try {
			
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			if(paramMap.get("courseId")!=null||paramMap.get("courseId")!=null){
				//搜素框赋值对象
				Map<String,Object> searchObejct = new HashMap<String,Object>();
				searchObejct.put("courseId", paramMap.get("courseId"));
				resultMap.put("searchObejct", searchObejct);
			}
			//通过课程id和当前登陆人所属老师的工号查询当前课程的试题
			Map<String,Object> returnMap = questionActionImpl.getOnlineQuestion(paramMap,session);
			resultMap.put("questonMap", returnMap.get("questonMap"));
			resultMap.put("courseList", returnMap.get("courseList"));
			resultMap.put("flag", returnMap.get("flag"));
			resultMap.put("errMsg", returnMap.get("errMsg"));
			
		} catch (Exception e) {
			resultMap.put("errMsg", "学生在线考试ajax请求出现异常");
			e.printStackTrace();
			logger.error("学生在线考试ajax请求出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
	
	/**在线批阅列表
	 * @param 获取当前登陆人所属学生在线考试列表（工号）
	 * @return  该教师的当前所属的所有班级学生考试情况
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getAllStuExamList", method = RequestMethod.GET)
	public ModelAndView getAllStuExamList(String jsonStr,HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {		
	
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(MemberAction.class.getName());
		try {
			//模拟参数开始
//			Map<String, Object> mapUse = new HashMap<String, Object>();
//			mapUse.put("pageNow", 1);
//			mapUse.put("pageSize", 10);
//			jsonStr = mapper.writeValueAsString(mapUse);
			//模拟参数结束
			//搜素框赋值对象
			Map<String,Object> searchObejct = new HashMap<String,Object>();
			//获取页面参数值
			Map<String, Object> preParamMap = mapper.readValue(jsonStr, resultMap.getClass());
			if(preParamMap.get("courseId")!=null||preParamMap.get("courseId")!=null){
				searchObejct.put("courseId", preParamMap.get("courseId"));
			}if(preParamMap.get("state")!=null||preParamMap.get("state")!=null){
				searchObejct.put("state", preParamMap.get("state"));
			}if(preParamMap.get("classId")!=null||preParamMap.get("classId")!=null){
				searchObejct.put("classId", preParamMap.get("classId"));
			}
			resultMap.put("searchObejct", searchObejct);
			//该教师的当前所属的所有班级学生考试情况
			Map<String,Object> findMap = questionActionImpl.getAllStuExamList(preParamMap,session);
			resultMap.put("memList", findMap.get("memList"));
			resultMap.put("courseList", findMap.get("courseList"));
			resultMap.put("classList", findMap.get("classList"));
			resultMap.put("totalPage", findMap.get("totalPage"));
			resultMap.put("pageNow", findMap.get("pageNow"));
		    //人列表页面名称
			pageName = "pc/allstuexamlist";
			
		} catch (Exception e) {
			resultMap.put("errMsg", "学生在线考试页面出现异常");
			e.printStackTrace();
			logger.error("学生在线考试页面出现异常", e);
			pageName = "public/pageerror";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
			System.out.println("返回的  :" + reJsonStr);
		}
		return new ModelAndView(pageName, returnMap);
	}
	
	/**
	 * @param request
	 * @param response
	 * @return  教师批阅ajax请求(前台传课程号，学生id)
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/markOnlineExam", method = RequestMethod.GET)
	public ModelAndView markOnlineExam(String jsonStr, HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放页面
		String pageName = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(ClassAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		//存放返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		try {
			
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//通过课程id和学号查询当前课程的试题
			Map<String,Object> finalMap = questionActionImpl.getMarkStuQuestion(paramMap);
			resultMap.put("question", finalMap.get("question"));
			//批阅页面名称
			pageName = "pc/markOnlineExam";
			
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(" 学生在线考试ajax请求出现异常", e);
			pageName = "public/pageerror";
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			returnMap.put("result", reJsonStr);
			System.out.println("返回的  :" + reJsonStr);
			
		}
		return new ModelAndView(pageName, returnMap);
	}
	
	/**
	 * @param request
	 * @param response
	 * @return  教师批阅点击是ajax请求表单提交(前台传课程号，学生id)
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/isExamSource", method = RequestMethod.POST)
	public void isExamSource(String jsonStr, HttpServletRequest request, HttpServletResponse response,HttpSession session) throws IOException {		
		
		//转换json格式工具
		ObjectMapper mapper = new ObjectMapper();
		//返回字符串
		String reJsonStr = "";
		//存放结果值
		Map<String,Object> resultMap = new HashMap<String,Object>();
		//日志信息
		Logger logger = Logger.getLogger(ClassAction.class.getName());
		//成功失败标识
		String resultMark = "fail";
		try {
			
			//获取参数值
			Map<String,Object> paramMap = mapper.readValue(jsonStr, resultMap.getClass());
			//将填空题、简答题错误个数和分数插入学生在线考试表考试总分数插入考试表中
			Map<String,Object> returnMap = questionActionImpl.addExameSource(paramMap,session);
			resultMap.put("resultMark", returnMap.get("resultMark"));
			
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(" 学生在线考试ajax请求出现异常", e);
		} finally {
			reJsonStr = mapper.writeValueAsString(resultMap);
			System.out.println("返回的："+reJsonStr);
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			write.println(reJsonStr);
			write.flush();
		}
	}
}
